#include "control/user_control.h"
#include "crypt/crypt_7th.h"
#include "our_header.h"
#include "tools/my_tlv.h"


int mkdir_control(int fd, MYSQL *conn, train_t *train)
{
    //解析客户端火车
    extra_data_t* ex_data=(extra_data_t*)train->data;//强转，获得火车头中extra_data_t
    char *dirname=train->data+sizeof(extra_data_t);//获得目录名

    train_t response;
    bzero(&response,sizeof(response));

    //token身份验证
    char token[4096]={0};
    getToken(ex_data->user,token);
    if(strcmp(token,ex_data->token))
    {
        response.cmdstat=MKDIR|RES|!IS_OK;
        strcpy(response.data,"token验证失败!\n");
        response.length=strlen(response.data)+1;
        send_train(fd, &response);
        return -1;
    }

    //调用创建函数
    int new_dir_id=0;
    int ret=makeDir(conn, ex_data->cur_dir_id, dirname,&new_dir_id );
    if(ret==0)
    {
        //创建成功
        response.cmdstat=MKDIR|RES|IS_OK;
        // ex_data->cur_dir_id=new_dir_id;
        memcpy(response.data,ex_data,sizeof(extra_data_t));
        char* success_msg=response.data+sizeof(extra_data_t);
        sprintf(success_msg,"目录创建成功,新目录ID:%d",new_dir_id);

        response.length=sizeof(extra_data_t)+strlen(success_msg)+1;
    }
    else
    {
        //创建失败
        response.cmdstat=MKDIR|RES|!IS_OK;
        ex_data->cur_dir_id=new_dir_id;
        memcpy(response.data,ex_data,sizeof(extra_data_t));
        char* error_msg=response.data+sizeof(extra_data_t);
        strcpy(error_msg,"目录创建失败");

        response.length=sizeof(extra_data_t)+strlen(error_msg)+1;
    }
    send_train(fd, &response);
    return 0;

}

int rmdir_control(int fd, MYSQL *conn, train_t *train) {
    //  解析客户端数据
    extra_data_t *ex_data = (extra_data_t *)train->data;
    char *dirname = train->data + sizeof(extra_data_t);  // 目录名在结构体之后

    train_t response;
    memset(&response,0,sizeof(response));

    //token身份验证
    char token[4096]={0};
    getToken(ex_data->user,token);
    if(strcmp(token,ex_data->token))
    {
        response.cmdstat=RMDIR|RES|!IS_OK;
        strcpy(response.data,"token验证失败!\n");
        response.length=strlen(response.data)+1;
        send_train(fd, &response);
        return -1;
    }

    // 调用removeDir函数
    int ret = removeDir(conn, ex_data->cur_dir_id, dirname);
    // 
    if(ret == 0) {
        response.cmdstat = RMDIR | RES | IS_OK;
        // dir_list_t current_list;
        // memset(&current_list, 0, sizeof(current_list));
        // getDirList(conn, ex_data->cur_dir_id, &current_list);

        // memcpy(response.data, ex_data, sizeof(extra_data_t));
        // memcpy(
        //        (char*)response.data + sizeof(extra_data_t), // 关键点：计算追加偏移量
        //        &current_list,                               // 注意取地址符&
        //        sizeof(dir_list_t)
        //       );

        //char *success_msg = response.data + sizeof(extra_data_t));
        char *success_msg = response.data + sizeof(extra_data_t);
        sprintf(success_msg, "目录删除成功");

        response.length = sizeof(extra_data_t) + strlen(success_msg);
    } else {
        response.cmdstat = RMDIR | RES | !IS_OK;

        // 保留当前目录信息
        memcpy(response.data, ex_data, sizeof(extra_data_t));
        char *error_msg = response.data + sizeof(extra_data_t);
        sprintf(error_msg, "目录删除失败: %s", dirname);

        response.length = sizeof(extra_data_t) + strlen(error_msg) + 1;
    }


    send_train(fd, &response);
    return 0;
}
